package com.wsg.lightaccounting.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.Model.CashCard;
import com.wsg.lightaccounting.Model.CreditCard;
import com.wsg.lightaccounting.PopupWindow.EditPopupWindow;
import com.wsg.lightaccounting.R;

import static com.wsg.lightaccounting.Activity.CashCardActivity.changeInput;

public class CreditCardActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView back, check;
    private LinearLayout bank, name, card_id, credit_add;
    private TextView bank_text, name_text, card_id_text, credit;
    private boolean isEdit = false;
    private long id = -1L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_card);
        Bundle bundle = this.getIntent().getExtras();
        isEdit = bundle.getBoolean("isEdit");
        intiView();
    }

    private void intiView() {
        back = (ImageView) findViewById(R.id.back);
        check = (ImageView) findViewById(R.id.check);

        bank = (LinearLayout) findViewById(R.id.bank_choose);
        card_id = (LinearLayout) findViewById(R.id.card_id_add);
        name = (LinearLayout) findViewById(R.id.name_add);
        credit_add = (LinearLayout) findViewById(R.id.credit_edit);

        bank_text = (TextView) findViewById(R.id.bank_name);
        card_id_text = (TextView) findViewById(R.id.card_id);
        name_text = (TextView) findViewById(R.id.name_wallet);
        credit = (TextView) findViewById(R.id.credit);

        back.setOnClickListener(this);
        check.setOnClickListener(this);

        bank.setOnClickListener(this);
        card_id.setOnClickListener(this);
        name.setOnClickListener(this);
        credit_add.setOnClickListener(this);

        if (isEdit) {
            Bundle bundle = this.getIntent().getExtras();
            bank_text.setText(bundle.getString("bank_name"));
            card_id_text.setText(bundle.getString("card_id"));
            name_text.setText(bundle.getString("name"));
            credit.setText(String.valueOf(bundle.getFloat("credit")));
            id = bundle.getLong("id");
            TextView textView = (TextView) findViewById(R.id.title);
            textView.setText("编辑信用卡");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.check:
               enter();
                MainActivity.updateAllUI(this);
                finish();
                break;
            case R.id.bank_choose:
                showEditPopupWinddow(bank_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            case R.id.card_id_add:
                showEditPopupWinddow(card_id_text,EditPopupWindow.INPUT_LIMIT_NUMBER);
                break;
            case R.id.name_add:
                showEditPopupWinddow(name_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            case R.id.credit_edit:
                showEditPopupWinddow(credit,EditPopupWindow.INPUT_LIMIT_FLAOT);
            default:
                break;
        }
    }
    private void showEditPopupWinddow(TextView textView,int i){
        EditPopupWindow editPopupWindow = new EditPopupWindow(this,this.findViewById(R.id.mark_size).getWidth(),
                this.findViewById(R.id.mark_size).getHeight(),textView,i);
        editPopupWindow.showAtLocation(this.findViewById(R.id.mark_size), Gravity.BOTTOM,0,0);
    }
    private void enter(){
        String name = String.valueOf(name_text.getText());
        String crad_id = String.valueOf(card_id_text.getText());
        String bank = String.valueOf(bank_text.getText());
        String credit1 = String.valueOf(credit.getText());
        float credit_f = 0f;
        if (!credit1.equals("")){
            credit_f = Float.parseFloat(credit1);
        }
        if (!isEdit) {
            AccountManager.getAccountManager(this).addCreditCard(new CreditCard(0L,crad_id,bank,name,credit_f,0L));
        } else {
            AccountManager.getAccountManager(this).updateCreditCard(id,bank,crad_id,name,credit_f);
        }

    }
}
